package com.example.mapdemon;

import android.app.Activity;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;


public class GpsSpeedActivity extends Activity {

private static final long MINIMUM_DISTANCE_CHANGE_FOR_UPDATES = 1; // in Meters	
private static final long MINIMUM_TIME_BETWEEN_UPDATES = 1000; // in Milliseconds
MyLocationListener listener = new MyLocationListener();

protected LocationManager locationManager;
	protected Button retrieveLocationButton;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_click);
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        setupBut();
       
    retrieveLocationButton = (Button) findViewById(R.id.retrieve_speed);
    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
      
    locationManager.requestLocationUpdates(
    LocationManager.GPS_PROVIDER,MINIMUM_TIME_BETWEEN_UPDATES, MINIMUM_DISTANCE_CHANGE_FOR_UPDATES, listener);		
    retrieveLocationButton.setOnClickListener(new OnClickListener() {
		
    @Override		
    public void onClick(View v) {
		showCurrentLocation();
	}});        
       
   }    

    protected void showCurrentLocation() {
        
    	Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
    	                
    	if (location != null) {
    	                        String message = String.format (
    	                                
    	        "Current Speed: %1$s", location.getSpeed());
    	                
    	        Toast.makeText(GpsSpeedActivity.this, message,
    	                        
    	                Toast.LENGTH_LONG).show();
    	                }
    	        }   

    	        private class MyLocationListener implements LocationListener {
    	                
    	public void onLocationChanged(Location location) {
    	                        
    	String message = String.format(
    	                                
    	        "Yor Speed is %1$s", location.getSpeed()+" ms");
    	        
    	                Toast.makeText(GpsSpeedActivity.this, message, Toast.LENGTH_LONG).show();
    	                }

    	                public void onStatusChanged(String s, int i, Bundle b) {
    	                        
    	Toast.makeText(GpsSpeedActivity.this, "Provider status changed",
    	                        
    	                Toast.LENGTH_LONG).show();
    	                }
    	                
    	public void onProviderDisabled(String s) {
    	                        
    	Toast.makeText(GpsSpeedActivity.this,
    	                        
    	                "Provider disabled by the user. GPS turned off",

    	                                        Toast.LENGTH_LONG).show();
    	                }
    	        
    	        public void onProviderEnabled(String s) {
    	                        
    	Toast.makeText(GpsSpeedActivity.this,
    	                        
    	                "Provider enabled by the user. GPS turned on",
    	        
    	                                Toast.LENGTH_LONG).show();
    	                }
    	        }
    	        
    	        
	private void setupBut() {
		Button button = (Button) findViewById(R.id.btnStopped);
		button.setOnClickListener(new OnClickListener() {
		   @Override
		   public void onClick(View v) {
			   locationManager.removeUpdates(listener);
			   locationManager=null;
			  finish();
		 }
		});
	   }
	
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}
	
}